<?php

namespace app\index\controller;

use think\Db;
use think\Request;

/**
 * 糖果机
 */
class Candy extends Base
{
    // 获取糖果机种类
    public function category()
    {
        $list = db('candym_type')->select();
        $this->result($list);
    }

    // 我的糖果机；
    public function myMachine()
    {   
        $map = [];
        $map['status'] = 0;
        $map['uid'] = $this->user['uid'];

        $list = model('CandyMachine')->with('candymType')
            ->where($map)->order('type')->select();
        
        $this->result($list);
    }

    // 我的糖果机购买记录
    public function getMachine()
    {
        $map = [];
        $map['uid'] = $this->user['uid'];
        if (input('param.uid')) {
            $map['uid'] = input('param.uid');
        }
        if (is_numeric(input('param.status'))) {
            $map['status'] = input('param.status');
        }

        $list = model('CandyMachine')->getList($map, input('param.size'));
        $this->result($list);
    }

    // 购买糖果机
    public function buyMachine()
    {
        $post = input('post.');
        $post['uid'] = $this->user['uid'];
        $rst = model('CandyMachine')->addInfo($post);
        $this->success($rst);
    }

    // 糖果机释放记录
    public function cmFreeLog()
    {
        $cm_id = input('param.cm_id');
        if ($cm_id) {
            $list = db('candym_log')->where('cm_id', $cm_id)->order('create_time DESC')->paginate(input('param.size'));
            return $this->result($list);
        }

        $this->error('糖果机不存在！');
    }


}